package com.ayoka.parenting.widget;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.ayoka.parenting.R;
import com.jincp.common.utils.UIUtils;

import java.util.ArrayList;

/**
 * 朋友圈点赞评论的popupwindow
 *
 * @author wei.yi
 */
public class TopicOperatorPopupWindow extends PopupWindow implements OnClickListener {

    private TextView btnTop;
    private TextView btnDelete;

    // 实例化一个矩形
    private Rect mRect = new Rect();
    // 坐标的位置（x、y）
    private final int[] location = new int[2];
    // 弹窗子类项选中时的监听
    private OnItemClickListener itemClickListener;
    // 定义弹窗子类项列表
    private ArrayList<String> actionItems = new ArrayList<>();

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public ArrayList<String> getActionItems() {
        return actionItems;
    }

    public void setActionItems(ArrayList<String> actionItems) {
        this.actionItems = actionItems;
    }


    public TopicOperatorPopupWindow(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.popup_topic_operate, null);
        btnTop = (TextView) view.findViewById(R.id.btn_top);
        btnDelete = (TextView) view.findViewById(R.id.btn_delete);
        btnTop.setOnClickListener(this);
        btnDelete.setOnClickListener(this);

        this.setContentView(view);
        this.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
        this.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.update();
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0000000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        this.setBackgroundDrawable(dw);
        this.setAnimationStyle(R.style.operation_popup_animation);
    }


    public void showPopupWindow(View parent) {
        if (actionItems.isEmpty()) return;
        int width = 0;
        int height = UIUtils.dp2px(40);
        for (int i = 0; i < actionItems.size(); i++) {
            String actionItem = actionItems.get(i);
            width += UIUtils.dp2px(70);
            if (i == 0) {
                btnDelete.setText(actionItem);
                btnDelete.setVisibility(View.VISIBLE);
            }
            if (i == 1) {
                btnTop.setText(actionItem);
                btnTop.setVisibility(View.VISIBLE);
                break;
            }
        }
        parent.getLocationOnScreen(location);
        // 设置矩形的大小
        mRect.set(location[0], location[1], location[0] + parent.getWidth(), location[1] + parent.getHeight());
        if (!this.isShowing()) {
            showAtLocation(parent, Gravity.NO_GRAVITY, location[0] - width, location[1] - ((height - parent.getHeight()) / 2));
        } else {
            dismiss();
        }
    }

    @Override
    public void onClick(View view) {
        dismiss();
        switch (view.getId()) {
            case R.id.btn_delete:
                itemClickListener.onItemClick(actionItems.get(0), 0);
                break;
            case R.id.btn_top:
                itemClickListener.onItemClick(actionItems.get(1), 1);
                break;
            default:
                break;
        }
    }

    /**
     * 添加子类项
     */
    public void addAction(String action) {
        if (action != null) {
            actionItems.add(action);
        }
    }

    /**
     * 功能描述：弹窗子类项按钮监听事件
     */
    public interface OnItemClickListener {
        void onItemClick(String item, int position);
    }
}
